package com.blunka.framework.client.constants;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.blunka.framework.client.db.entity.BatchSqlEntity;
import com.blunka.framework.client.utils.DesignUtils;

/**
 * 初始化设计器基础配置文件，系统配置文件和数据库配置文件
 * @author xuqiang
 *
 */
public class InitDesignConfig {

	private static final Logger logger = Logger.getLogger(InitDesignConfig.class);

	/**
	 * 系统配饰信息
	 */
	private static Document doc;

	/**
	 * 本地缓存数据
	 */
	private static Document dbLocalDoc;

	/**
	 * 服务器数据库缓存
	 */
	private static Document dbServerDoc;
	
	public static final String MYSQL = "MYSQL";
	
	public static final String ORACLE = "ORACLE";
	
	public static final String MSSQL = "MSSQL";

	static{
		SAXReader reader = new SAXReader();
		try{
			doc = reader.read(new File(DesignUtils.getDir()+"/conf/systemConfig.xml"));
			logger.info("加载基础数据完毕");
			//判断数据库类型
			String dbType = doc.getRootElement().element("dbSetting").elementText("dbType");
			String dbFile = "";
			switch(dbType){
				case "MYSQL":{
					dbFile = "mysql_sql.xml";
					break;
				}
				case "ORACLE":{
					dbFile = "oracle_sql.xml";
					break;
				}
				case "MSSQL":{
					dbFile = "mssql_sql.xml";
					break;
				}
				default:{
					logger.error("没有配置数据库类型，初始化系统数据失败,系统自动退出");
					System.exit(0);
				}
			}
			//本地缓存sql
			dbLocalDoc = reader.read(new File(DesignUtils.getDir()+"/conf/sql/sqllist_sql.xml"));
			//服务端数据库sql
			dbServerDoc = reader.read(new File(DesignUtils.getDir()+"/conf/sql/"+dbFile));

		}catch(Exception e){
			logger.error("加载基础数据异常,系统自动退出",e);
			System.exit(0);
		}
	}
	
	/**
	 * 获取设计器标题
	 * @return
	 */
	public static String getDesignerName(){
		Element e =doc.getRootElement().element("baseSetting");
		return e.elementText("designName");
	}

	/**
	 * 获取设计器版本号
	 * @return
	 */
	public static String getDesignerVersion(){
		Element e =doc.getRootElement().element("baseSetting");
		return e.elementText("version");
	}

	/**
	 * 获取应用建模根节点名称
	 * @return
	 */
	public static String getModelRootName(){
		Element e =doc.getRootElement().element("modelSetting");
		return e.elementText("modelRootName");
	}

	/**
	 * 获取系统建模表前缀
	 * @return
	 */
	public static String getSystemTablePrefix(){
		Element e =doc.getRootElement().element("modelSetting");
		return e.elementText("sysPrefix");
	}

	/**
	 * 获取系统建模试图前缀
	 * @return
	 */
	public static String getSystemViewPrefix(){
		Element e =doc.getRootElement().element("modelSetting");
		return e.elementText("sysViewPrefix");
	}

	/**
	 * 获取建模，建表节点前缀
	 * @return
	 */
	public static String getTablePrefix(){
		Element e =doc.getRootElement().element("modelSetting");
		return e.elementText("tablePrefix");
	}

	/**
	 * 获取模型 试图节点前缀
	 * @return
	 */
	public static String getViewPrefix(){
		Element e =doc.getRootElement().element("modelSetting");
		return e.elementText("viewPrefix");
	}

	/**
	 * 获取数据库类型
	 * MYSQL,ORACLE,MSSQL
	 * @return
	 */
	public static String getDBType(){
		Element e =doc.getRootElement().element("dbSetting");
		return e.elementText("dbType");
	}

	/**
	 * 获取本地缓存数据库初始化语句
	 * @return
	 */
	public static List<String> getLocalDBSql(){
		Element e =dbLocalDoc.getRootElement().element("sqllist");
		List<?> list  = e.elements("sql");
		List<String> sqlList = new ArrayList<String>();
		for(int i=0;i<list.size();i++){
			Element sqlEl = (Element)list.get(i);
			String sql = sqlEl.elementText("sqltext");
			sqlList.add(sql);
		}
		return sqlList;
	}
	
	/**
	 * 获取本地初始化数据
	 * @return
	 */
	public static List<BatchSqlEntity> getLocalDBDataSql(){
		Element e =dbLocalDoc.getRootElement().element("sqldatalist").element("sqldatafile");
		String file_name = e.getTextTrim();
		if(StringUtils.isEmpty(file_name)) {
			return null;
		}
		List<BatchSqlEntity> sqlList = new ArrayList<BatchSqlEntity>();
		try {
			File file = new File(DesignUtils.getDir()+"/conf/sql/data/"+file_name);
			if(!file.exists()) {
				return null;
			}
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			String sql_str = "";
			while ((sql_str = br.readLine()) != null) {
				if(!StringUtils.isEmpty(sql_str.trim())) {
					BatchSqlEntity sqlEntity = new BatchSqlEntity();
					sqlEntity.setSql(sql_str);
					sqlList.add(sqlEntity);
				}
			}
			 br.close();
		     isr.close();
		     fis.close();
		}catch(Exception ex) {
			logger.error("加载初始化数据文件异常",ex);
		}
		return sqlList;
	}

	/**
	 * 获取服务器初始化数据库建表语句
	 * @return
	 */
	public static List<String> getServerDBSql(){
		Element e =dbServerDoc.getRootElement().element("sqllist");
		List<?> list  = e.elements("sql");
		List<String> sqlList = new ArrayList<String>();
		for(int i=0;i<list.size();i++){
			Element sqlEl = (Element)list.get(i);
			String sql = sqlEl.elementText("sqltext");
			sqlList.add(sql);
		}
		return sqlList;
	}
	
	/**
	 * 获取服务器初始化数据
	 * @return
	 */
	public static List<BatchSqlEntity> getServerDBDataSql(){
		Element e =dbServerDoc.getRootElement().element("sqldatalist").element("sqldatafile");
		String file_name = e.getTextTrim();
		if(StringUtils.isEmpty(file_name)) {
			return null;
		}
		List<BatchSqlEntity> sqlList = new ArrayList<BatchSqlEntity>();
		try {
			File file = new File(DesignUtils.getDir()+"/conf/sql/data/"+file_name);
			if(!file.exists()) {
				return null;
			}
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			String sql_str = "";
			while ((sql_str = br.readLine()) != null) {
				if(!StringUtils.isEmpty(sql_str.trim())) {
					BatchSqlEntity sqlEntity = new BatchSqlEntity();
					sqlEntity.setSql(sql_str);
					sqlList.add(sqlEntity);
				}
			}
			 br.close();
		     isr.close();
		     fis.close();
		}catch(Exception ex) {
			logger.error("加载初始化数据文件异常",ex);
		}
		return sqlList;
	}

	/**
	 * 获取服务器数据语句配置文件根节点
	 * @return
	 */
	public static Element getServerDBSqlRoot(){
		return dbServerDoc.getRootElement();
	}
}
